#!/usr/bin/python
# -*- coding: UTF-8 -*-

import json
import json
from datetime import time

import numpy as np
from ..items import CategorieItem


def parseFile(file_path, data):
    # # obj_dic = class2dic(data)
    # # print(json.dumps(obj_dic))
    # keys = [str(x) for x in np.arange(len(data))]
    # list_json = dict(zip(keys, data))
    # # str_json = json.dumps(list_json, default=DhgateCategorieItem.obj_to_json())  # json转为string
    # str_json = json.dumps(list_json, ensure_ascii=False)  # json转为string
    # print(str_json)
    # return str_json
    pass


def list_to_json(data):
    dataLst = []
    # for item in  data:
    #
    # keys = [str(x) for x in np.arange(len(data))]
    # list_json = dict(zip(keys, data))
    # str_json = json.dumps(list_json, indent=2, ensure_ascii=False)  # json转为string
    # str_json = json.dumps(data, cls=DhgateCategorieItem.obj_to_json())
    str_json = json.dumps(data, default=CategorieItem.obj_to_json)
    print(str_json)
    return str_json


class MyEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, np.integer):
            return int(obj)
        elif isinstance(obj, np.floating):
            return float(obj)
        elif isinstance(obj, np.ndarray):
            return obj.tolist()
        if isinstance(obj, time):
            return obj.__str__()
        else:
            return super(MyEncoder, self).default(obj)


class DateEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, time):
            return obj.__str__()
        return json.JSONEncoder.default(self, obj)


def student2dict(data):
  return {
  'name':data.name,
  'age':data.age,
  'score':data.score
}


# def class2dic(obj):
#     obj_dic = obj.__dict__
#
#     for key in obj_dic.keys():
#         value = obj_dic[key]
#
#         obj_dic[key] = value2py_data(value)
#
#     return obj_dic
#
#
# def value2py_data(value):
#     if str(type(value)).__contains__('__main__'):
#
#         # value 为自定义类
#
#         value = class2dic(value)
#
#     elif str(type(value)) == "<class 'list'>":
#
#         # value 为列表
#
#         for index in range(0, value.__len__()):
#             value[index] = value2py_data(value[index])
#
#     return value
